package datastructure;

public class LinkedList <E>{
	private class Node <E>{
		protected Node next;
		protected Object data;
		public int date;
		public Node(E data) {
			this.data=data;
		}
	}
	private static final int E = 0;
	public Node first;
	private int index;
	public LinkedList() {
		this.first=null;
		this.index=-1;
	}
	public boolean isIndex(int pos) {
		if(pos>=0&&pos<=this.index) {
			return true;
		}else {
			throw new RuntimeException("......");
		}
	}
	public boolean add(E e) {
		if(this.first==null) {
			this.first.data=e;
			index++;
		}else {
			Node temp=first;
			while(temp.next!=null) {
				temp=temp.next;
			}
			temp.next=new Node(e);
			index++;
		}
		return true;
	}
	public boolean insert(int pos,E e) {
		this.isIndex(pos);
		Node temp=first;
		for(int i=0;i<pos;i++) {
			temp=temp.next;
		}
		Node n=new Node(e);
		n.next=temp.next;
		temp.next=n;
		return true;
	}
	public E delete(int pos) {
		this.isIndex(pos);
		Node temp=first;
		while(temp.next!=null) {
			temp=temp.next;
		}
		E result=(E) temp.data;
		temp=null;
		index--;
		return result;
	}
	public boolean set(int pos,E e) {
		this.isIndex(pos);
		Node temp=first;
		for(int i=0;i<pos;i++) {
			temp=temp.next;
		}
		temp.next.data=e;
		return true;
	}
	public E get(int pos) {
		this.isIndex(pos);
		Node temp=first;
		for(int i=0;i<pos;i++) {
			temp=temp.next;
		}
		return (E)temp.data;
	}
	public int size() {
		return index;
	}
	
}